package com.hsq.ndmall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hsq.ndmall.product.dao.GoodsProductDao;
import com.hsq.ndmall.product.entity.GoodsProductEntity;
import com.hsq.ndmall.product.service.GoodsProductService;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

@Service("goodsProductService")
public class GoodsProductServiceImpl extends ServiceImpl<GoodsProductDao, GoodsProductEntity> implements GoodsProductService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsProductEntity> page = this.page(
                new Query<GoodsProductEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    private QueryWrapper getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<GoodsProductEntity> queryWrapper = new QueryWrapper<>();
        if (params.containsKey("goods_id")) {
            queryWrapper.lambda().eq(GoodsProductEntity::getGoodsId, params.get("goods_id"));
        }
        if (params.containsKey("id")) {
            queryWrapper.lambda().eq(GoodsProductEntity::getId, params.get("id"));
        }
        return queryWrapper;
    }
}
